package labrom.usage.server;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;

import labrom.usage.shared.Props;

public class PropsLoader {
	
	Props props;
	
	private static final String TYPE_ID_PREFIX = "typeId.";
	private static final String SCREEN_ID_PREFIX = "screenId.";

	public void loadFromProperties(InputStream in) throws IOException {
		Properties p = new Properties();
		p.load(in);
		in.close();
		
		props = new Props();
		props.pieChartSliceThreshold = Float.parseFloat(p.getProperty("pieChartSliceThreshold", String.valueOf(props.pieChartSliceThreshold)));
		props.pieChartHeight = Integer.parseInt(p.getProperty("pieChartHeight", String.valueOf(props.pieChartHeight)));
		props.pieChartWidth = Integer.parseInt(p.getProperty("pieChartWidth", String.valueOf(props.pieChartWidth)));
		props.pieChartLegendFontSize = Double.parseDouble(p.getProperty("pieChartLegendFontSize", String.valueOf(props.pieChartLegendFontSize)));
		props.excludedGuids.addAll(new HashSet<String>(Arrays.asList(p.getProperty("excludedGuids", "").split(","))));
		
		for(Map.Entry<Object, Object> e : p.entrySet()) {
			String k = e.getKey().toString();
			if(k.startsWith(TYPE_ID_PREFIX)) {
				props.typeCaptions.put(new Integer(k.substring(TYPE_ID_PREFIX.length())), e.getValue().toString());
			} else if(k.startsWith(SCREEN_ID_PREFIX)) {
				props.screenCaptions.put(new Integer(k.substring(SCREEN_ID_PREFIX.length())), e.getValue().toString());
			}
		}
		
		props.queryMaxResults = Integer.parseInt(p.getProperty("queryMaxResults", String.valueOf(props.queryMaxResults)));
		
		props.appVersionMin = Integer.parseInt(p.getProperty("appVersionMin", String.valueOf(props.appVersionMin)));
		props.appVersionMax = Integer.parseInt(p.getProperty("appVersionMax", String.valueOf(props.appVersionMax)));
	}
	
	public Props getProps() {
		return props;
	}

}
